﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Algobox.JuicyLib.Utils
{
    public class JuicyTime
    {
        public enum DateFormat
        {
            CCYYMMDD,
            CCYYMMDDHHMMSS
        }

        public static bool ConvertStringToDateTime(DateFormat format, string sDateTime, out DateTime time)
        {
            bool ret = false;

            time = DateTime.MinValue;

            switch (format)
            {
                case DateFormat.CCYYMMDD:
                    {
                        try
                        {
                            int nDays = int.Parse(sDateTime.Substring(6, 2));
                            int nMonth = int.Parse(sDateTime.Substring(4, 2));
                            int nYears = int.Parse(sDateTime.Substring(0, 4));

                            time = new DateTime(nYears, nMonth, nDays);
                        }
                        catch (Exception e)
                        {
                            //error on parse
                            ret = false;
                        }
                        break;
                    }
                case DateFormat.CCYYMMDDHHMMSS:
                    {
                        try
                        {
                            int nYears = int.Parse(sDateTime.Substring(0, 4));
                            int nMonth = int.Parse(sDateTime.Substring(4, 2));
                            int nDays = int.Parse(sDateTime.Substring(6, 2));
                            
                            int nHours = int.Parse(sDateTime.Substring(8, 2));
                            int nMinutes = int.Parse(sDateTime.Substring(10, 2));
                            int nSecond = int.Parse(sDateTime.Substring(12, 2));

                            time = new DateTime(nYears, nMonth, nDays,nHours,nMinutes,nSecond);
                        }
                        catch (Exception e)
                        {
                            //error on parse
                            ret = false;
                        }
                        break;
                    }
                default:
                    {
                        //format not supported
                        ret = false;
                        break;
                    }
            }
            //if (ret == false)
            //{
                //time = DateTime.Now;  
            //}
            return ret;
        }
    }
}
